package com.wyx.mhl.service;

import com.wyx.mhl.dao.BillDao;
import com.wyx.mhl.dao.DiningTableDao;
import com.wyx.mhl.dao.MultiTableBeanDao;
import com.wyx.mhl.domain.bill;
import com.wyx.mhl.domain.multiTableBean;

import java.util.List;
import java.util.UUID;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class BillService {
    private BillDao billDao = new BillDao();
    private MultiTableBeanDao multiTableBeanDao = new MultiTableBeanDao();
    private MenuService menuService = new MenuService();
    private DiningTableService diningTableService = new DiningTableService();

    //编写方法,生成账单并且更新餐桌信息
    public boolean order(int menuID, int nums, int tableId) {
        //使用UUID生成一个账单号
        String billId = UUID.randomUUID().toString();
        int update = billDao.update("insert into bill values(null,?,?,?,?,?,now(),'未结账')", billId, menuID, nums, menuService.getSingleMenu(menuID).getPrice() * nums, tableId);
        if (update <= 0) {
            return false;
        }

        //更改餐桌状态为就餐中
        return diningTableService.updateDiningTableState(tableId, "就餐中");
    }


    //编写方法，输出账单信息
    public List<bill> getAllBill(){
        return billDao.queryMulti("SELECT * FROM bill",bill.class);
    }

    //编写方法，输出账单信息包括菜品名
    public List<multiTableBean> getAllBill2(){
        return multiTableBeanDao.queryMulti("SELECT bill.*,`name` FROM bill,menu WHERE bill.menuId = menu.id", multiTableBean.class);
    }


    //编写方法，根据桌位号返回消费总金额
    public double total(int tableId){
        Object total = billDao.queryScalar("SELECT SUM(money) FROM bill WHERE tableId = ? AND billState =?", tableId, "未结账");
        if(total == null){
            total = 0.0;
        }
        return (double)total;

    }

    //编写方法，修改bill表中账单状态为支付方式，并初始化diningTable表中对应的桌位
    public boolean payBill(int tableId,String payMode){
        //修改bill表中账单状态为支付方式
        int update = billDao.update("update bill set billState = ? where tableId = ? and billState = ?", payMode, tableId, "未结账");
        if(update <= 0){
            return false;
        }

        //初始化diningTable表中对应的桌位
        if(diningTableService.updateDiningTableStateToFree(tableId)){
            return true;
        }else {
            return false;
        }

    }


}
